<template>
	<view>
		<view v-if="!authflag.getPhoneNumber || authflag.getPhoneNumber == false || btnPath == 'changePhone'">
			<button class="slot-btn" type="default" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber" withCredentials="true" hover-class="none">
				<slot></slot>
			</button>
		</view>
		<view v-else>
			<slot></slot>
		</view>
	</view>
</template>

<script>
	import {mapState} from 'vuex'
	export default {
		name: 'wx-auth',
		props: {
			authType: {
				type: Array,
				default(){
					return ['authAvatar']
				}
			},
			btnPath: {
				type: String,
				default(){
					return 'auth'
				}
			},
		},
		computed:{
			...mapState(['userinfo', 'authflag'])
		},
		data() {
			return {
				
			};
		},
		methods: {
			getPhoneNumber(e){
				if(e.detail.errMsg=="getPhoneNumber:ok"){
					uni.showLoading({
						title: "绑定手机号中",
						icon: "none"
					})
					let params = {
						bind_token: this.userinfo.token,
						encrypted_data:e.detail.encryptedData,
						iv: e.detail.iv
					}
					this.$api.setUserPhone(params).then(res => {
						let timestamp = Date.parse(new Date());
						console.log('获取 token  的时间戳', timestamp)
						let tokenRefreshTime = (res.expire_in -  120) * 1000 + timestamp
						this.$store.dispatch("set_token_refresh_time", tokenRefreshTime)
						this.$store.dispatch("set_userinfo", res)
						let getUserInfo = false
						let getPhoneNumber = false
						if(res.avatar !='' && res.nickname !='') getUserInfo = true
						if(res.mobile !='') getPhoneNumber = true
						console.log(res.uid)
						this.$store.dispatch("set_authflag", {
							getUserInfo, 
							getPhoneNumber
						})
						
						if(res.uid && res.uid != 0) {
							console.log('标识 已经有了啊')
							this.$api.getTaskToken({
								code: res.token
							}).then(result => {
								this.$store.dispatch("set_task_userinfo", result)
								uni.$emit('redresh_ucenter')
								if(result){
									uni.$emit('refresh_list')
									if(result.is_sku_actived == 1) {
										this.$store.dispatch("set_authflag", {
											hasShop: true 
										})
									}
									if(result.douyin_id!=''){
										this.$store.dispatch("set_authflag", {
											bindDouyin: true 
										})
									}else{
										this.$store.dispatch("set_authflag", {
											bindDouyin: false 
										})
									}
								}
							})
						}
						
						uni.hideLoading()
						if(this.btnPath == 'bindDouyin') 
						uni.$emit('phone_confirm') // 触发绑定手机号动作
						this.$store.dispatch("set_authflag", {
							getPhoneNumber: true
						})
					})
				}else{
					uni.showToast({
						title: "需要登录",
						icon: "none"
					})
				}
			},
			
		}
	}
</script>

<style lang="scss">
	.slot-btn{
		background-color: rgba(0,0,0,0)!important;
		padding: 0;margin: 0;
		&::after {
			border: none;
		}
		&.button-hover {
			color:rgba(0, 0, 0, 0.6);
			background-color:#fff;
		}
	}
</style>
